library(osmdata)
library(opentripplanner)
library(tidyverse)
library(sf)
library(ggthemes)
library(ggspatial)
library(dplyr)
library(plyr)
In this exercise, I will look at hubway stations and landmarks in Downtown Boston, MA.
hubway_stations <- st_read(
"http://bostonopendata-boston.opendata.arcgis.com/datasets/ee7474e2a0aa45cbbdfe0b747a5eb032_0.kml?outSR=%7B%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%7D")
## Reading layer `Hubway_Stations' from data source `http://bostonopendata-boston.opendata.arcgis.com/datasets/ee7474e2a0aa45cbbdfe0b747a5eb032_0.kml?outSR=%7B%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%7D' using driver `KML'
## Simple feature collection with 191 features and 2 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -71.16649 ymin: 42.30347 xmax: -71.0061 ymax: 42.4063
## geographic CRS: WGS 84
landmarks <- st_read("http://bostonopendata-boston.opendata.arcgis.com/datasets/7a7aca614ad740e99b060e0ee787a228_3.kml")
## Reading layer `Boston_Landmarks_Commission__BLC__Landmarks' from data source `http://bostonopendata-boston.opendata.arcgis.com/datasets/7a7aca614ad740e99b060e0ee787a228_3.kml' using driver `KML'
## Simple feature collection with 172 features and 2 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -71.18994 ymin: 42.25131 xmax: -71.02662 ymax: 42.38096
## geographic CRS: WGS 84
Street network data from Open Street Map
opq(bbox = 'Downtown Boston MA USA') %>%
add_osm_feature(key = 'highway') %>%
osmdata_xml(file = 'OTP/graphs/default/downtownb_streets.osm')
SF features from Open Street Map to plot maps.
DB_state_plane <- "+proj=lcc +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000 +ellps=GRS80 +units=m +no_defs"
downtown_street_features <- opq(bbox = 'Downtown Boston MA USA') %>%
add_osm_feature(key = 'highway') %>%
osmdata_sf()
downtown_streets <- downtown_street_features$osm_lines %>%
st_transform(crs = DB_state_plane)
Plotting downloaded streets
ggplot(downtown_streets) +
geom_sf() +
theme_map()+
annotation_north_arrow(location = "tl", height = unit(0.7, "cm"),
width = unit(0.7, "cm") )
OTP Setup
path_otp <- otp_dl_jar("OTP")
Street and transit networks
path_data <- file.path(getwd(), "OTP")
path_otp <- paste(path_data, "otp.jar",sep = "/")
otp_build_graph(otp = path_otp, dir = path_data, memory = 1024)
otp_setup(otp = path_otp, dir = path_data, memory =1024)
Connecting to OpenTripPlanner
otpcon <- otp_connect()
iso_5min_walk <-
otp_isochrone(otpcon = otpcon, fromPlace = hubway_stations,
mode = "WALK", cutoffSec = 300) %>%
st_transform(crs = DB_state_plane) %>%
mutate(mode = "walk")
## Warning in otp_isochrone(otpcon = otpcon, fromPlace =
## hubway_stations, mode = "WALK", : Failed to get isochrone with error:
## org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ffe"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ffd"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ffc"}]}Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ff9"}]}Failed to get
## isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ff8"}]}Failed to get
## isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ff7"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ff5"}]}Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7ff4"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fec"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fea"}]}Failed to get
## isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fe7"}]}Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fe4"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices not
## found: [from] vertices not found: [from]Failed to get isochrone with error:
## org.opentripplanner.routing.error.VertexNotFoundException: vertices not found:
## [from] vertices not found: [from]Failed to
iso_5min_bike <-
otp_isochrone(otpcon = otpcon, fromPlace = hubway_stations,
mode = "BICYCLE", cutoffSec = 300) %>%
st_transform(crs = DB_state_plane) %>%
mutate(mode = "bike")
## Warning in otp_isochrone(otpcon = otpcon, fromPlace = hubway_stations,
## mode = "BICYCLE", : Failed to get isochrone with error:
## org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fd3"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fd2"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fd1"}]}Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fce"}]}Failed to get
## isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fcd"}]}Failed to get
## isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fcc"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fca"}]}Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fc9"}]}Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fc7"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fc1"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone
## with error: org.opentripplanner.routing.error.VertexNotFoundException:
## vertices not found: [from] vertices not found: [from]Failed to
## get isochrone with error: {"type":"FeatureCollection","features":
## [{"type":"Feature","geometry":null,"properties":{"time":
## 300},"id":"fid-71628b06_1750081dc96_-7fb9"}]}Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices
## not found: [from] vertices not found: [from]Failed to get isochrone with
## error: org.opentripplanner.routing.error.VertexNotFoundException: vertices not
## found: [from] vertices not found: [from]Failed to get isochrone with error:
## org.opentripplanner.routing.error.VertexNotFoundException: vertices not found:
## [from] vertices not found: [from]Failed to get isochrone with error: or
iso_all_modes <- rbind(iso_5min_bike, iso_5min_walk)
otp_stop()
## character(0)
right_side <- st_bbox(iso_all_modes)$xmax
left_side <- st_bbox(iso_all_modes)$xmin
top_side <- st_bbox(iso_all_modes)$ymax
bottom_side <- st_bbox(iso_all_modes)$ymin
ggplot(iso_all_modes) +
annotation_map_tile(zoomin = 1, progress = "none") +
geom_sf(aes(fill = mode), alpha = 0.2) +
geom_sf(data = hubway_stations) +
coord_sf(xlim = c(left_side, right_side),
ylim = c(bottom_side, top_side), expand = FALSE) +
scale_fill_viridis_d(name = "Area that is reachable within 5 minutes",
labels = c("By bike", "By foot")) +
annotation_scale(location = "br")+
theme_map() +
labs(caption = "Basemap Copyright OpenStreetMap contributors")
## Loading required namespace: raster
ggplot(iso_all_modes) +
annotation_map_tile(zoomin = 1, type= "cartolight", progress = "none") +
geom_sf(aes(fill = mode), alpha = 0.2) +
geom_sf(data = hubway_stations) +
coord_sf(xlim = c(left_side, right_side),
ylim = c(bottom_side, top_side), expand = FALSE) +
scale_fill_viridis_d(name = "Area that is reachable within 5 minutes",
labels = c("By bike", "By foot"),
option = "plasma") +
annotation_scale(location = "br")+
theme_map() +
labs(caption = "Basemap Copyright OpenStreetMap contributors")
iso_areas <- iso_all_modes %>%
mutate(area = st_area(iso_all_modes)) %>%
st_set_geometry(NULL) %>%
pivot_wider(names_from = mode, values_from = area)
ggplot(iso_areas,
aes(x = as.numeric(walk), y = as.numeric(bike))) +
geom_point() +
stat_smooth(color = "black", linetype = 2, size = 0.5) +
scale_x_continuous(name =
"Area within a five-minute walking distance of a hubway station\n(square km)",
breaks = breaks <- seq(10000, 130000, by = 20000),
labels = breaks / 1000000) +
scale_y_continuous(name =
"Area within a five-minute biking distance of a hubway station\n(square km)",
breaks = breaks <- seq(0, 700000, by = 100000),
labels = breaks / 1000000) +
ggtitle("Downtown Boston", subtitle = paste0("hubway stations"))+
theme_pander()
## Warning: Removed 6 rows containing non-finite values (stat_smooth).
## Warning: Removed 6 rows containing missing values (geom_point).
landmarks <- landmarks %>%
st_transform(DB_state_plane)
landmarks <- landmarks[iso_5min_bike,]
ggplot(iso_5min_bike) +
geom_sf(color= NA) +
geom_sf(data = landmarks,
fill = "black", color= NA) +
theme_map()
right_side <- st_bbox(iso_all_modes)$xmax
left_side <- st_bbox(iso_all_modes)$xmin
top_side <- st_bbox(iso_all_modes)$ymax
bottom_side <- st_bbox(iso_all_modes)$ymin
ggplot(iso_5min_bike) +
annotation_map_tile(zoomin = 1, type = "cartolight", progress = "none") +
geom_sf(aes(fill = mode), alpha = 0.2) +
geom_sf(data = landmarks, fill= "black", color= NA, size = 1.5) +
coord_sf(xlim = c(left_side, right_side),
ylim = c(bottom_side, top_side), expand = FALSE) +
scale_fill_viridis_d(name = "Area that is reachable within 5 minutes",
labels = c("By bike")) +
annotation_scale(location = "br")+
theme_map() +
labs(caption = "Basemap Copyright OpenStreetMap contributors")
otp_stop()
## The following Java instances have been found:
## Warning in system("pkill -9 java", intern = TRUE): running command 'pkill -9
## java' had status 1
## character(0)
## attr(,"status")
## [1] 1